package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialwarehousingMaterialExtEntity;
import city.spring.modules.material.repository.MaterialwarehousingMaterialExtRepository;
import city.spring.modules.material.service.MaterialwarehousingMaterialExtService;
import city.spring.modules.material.vo.MaterialwarehousingMaterialVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：物资入库与货架关联
 *
 * @author 谢金婷
 * @date 2020-07-17 11:44:42
 */
@CacheConfig(cacheNames = {MaterialwarehousingMaterialExtServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MaterialwarehousingMaterialExtServiceImpl extends ServiceImpl<MaterialwarehousingMaterialExtRepository, MaterialwarehousingMaterialExtEntity> implements MaterialwarehousingMaterialExtService {

    private final MaterialwarehousingMaterialExtRepository materialwarehousingMaterialExtRepository;

    public MaterialwarehousingMaterialExtServiceImpl(MaterialwarehousingMaterialExtRepository materialwarehousingMaterialExtRepository) {
        this.materialwarehousingMaterialExtRepository = materialwarehousingMaterialExtRepository;
    }

    @Override
    public void saveMaterialwarehousingMaterialExt(MaterialwarehousingMaterialExtEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateMaterialwarehousingMaterialExt(MaterialwarehousingMaterialExtEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteMaterialwarehousingMaterialExt(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteMaterialwarehousingMaterialExt(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public IPage<MaterialwarehousingMaterialExtEntity> getByMaterialId(Page<? extends MaterialwarehousingMaterialExtEntity> page, Wrapper<? extends MaterialwarehousingMaterialExtEntity> wrapper, String materialId) {
        return materialwarehousingMaterialExtRepository.getByMaterialId(page, wrapper, materialId);
    }

    @Override
    public List<MaterialwarehousingMaterialVO> getWarehousingMaterials(String materialwarehousingId) {
        return materialwarehousingMaterialExtRepository.getWarehousingMaterials(materialwarehousingId);
    }

    @Override
    public IPage<MaterialwarehousingMaterialExtEntity> getMaterialWarehouseList(Page<? extends MaterialwarehousingMaterialExtEntity> page, String storehouseId, String materialId) {
        return materialwarehousingMaterialExtRepository.getMaterialWarehouseList(page, storehouseId, materialId);
    }

}